###########################################################################
#                            TextMate Snippets                            #
###########################################################################

priority -50

snippet def "#define ..."
#define ${1}
endsnippet

snippet ifndef "#ifndef ... #define ... #endif"
#ifndef ${1/([A-Za-z0-9_]+).*/$1/}
#define ${1:SYMBOL} ${2:value}
#endif
endsnippet

snippet #if "#if #endif" b
#if ${1:0}
${VISUAL}${0}
#endif
endsnippet

snippet inc "#include local header (inc)"
#include "${1:`!p snip.rv = snip.basename + '.h'`}"
endsnippet

snippet Inc "#include <> (Inc)"
#include <${1:.h}>
endsnippet

snippet mark "#pragma mark (mark)"
#if 0
${1:#pragma mark -
}#pragma mark $2
#endif

$0
endsnippet

snippet main "main() (main)"
int main(int argc, char *argv[])
{
	${VISUAL}${0}
	return 0;
}
endsnippet

snippet for "for loop (for)"
for (${2:i} = 0; $2 < ${1:count}; ${3:++$2}) {
	${VISUAL}${0}
}
endsnippet

snippet fori "for int loop (fori)"
for (${4:int} ${2:i} = 0; $2 < ${1:count}; ${3:++$2}) {
	${VISUAL}${0}
}
endsnippet

snippet enum "Enumeration"
enum ${1:name} { $0 };
endsnippet

snippet once "Include header once only guard"
#ifndef ${1:`!p
if not snip.c:
	import random, string
	name = re.sub(r'[^A-Za-z0-9]+','_', snip.fn).upper()
	rand = ''.join(random.sample(string.ascii_letters+string.digits, 8))
	snip.rv = ('%s_%s' % (name,rand)).upper()
else:
	snip.rv = snip.c`}
#define $1

${VISUAL}${0}

#endif /* end of include guard: $1 */
endsnippet

snippet td "Typedef"
typedef ${1:int} ${2:MyCustomType};
endsnippet

snippet wh "while loop"
while(${1:/* condition */}) {
	${VISUAL}${0}
}
endsnippet

snippet do "do...while loop (do)"
do {
	${VISUAL}${0}
} while(${1:/* condition */});
endsnippet

snippet fprintf "fprintf ..."
fprintf(${1:stderr}, "${2:%s}\n"${2/([^%]|%%)*(%.)?.*/(?2:, :\);)/}$3${2/([^%]|%%)*(%.)?.*/(?2:\);)/}
endsnippet

snippet eli "else if .. (eli)"
else if (${1:/* condition */}) {
	${VISUAL}${0}
}
endsnippet

snippet printf "printf .. (printf)"
printf("${1:%s}\n"${1/([^%]|%%)*(%.)?.*/(?2:, :\);)/}$2${1/([^%]|%%)*(%.)?.*/(?2:\);)/}
endsnippet

snippet st "struct"
struct ${1:`!p snip.rv = (snip.basename or "name") + "_t"`} {
	${0:/* data */}
};
endsnippet

snippet fun "function" b
${1:void} ${2:function_name}(${3})
{
	${VISUAL}${0}
}
endsnippet

snippet fund "function declaration" b
${1:void} ${2:function_name}(${3});
endsnippet

######################################################################
# magc for stdio
######################################################################

snippet fopen "FILE *<--fopen <----stdio.h" b
fopen(${1:/*const char *filename*/} ,${2:/* const char *mode*/})
endsnippet

snippet fwrite "size_t <--fwrite <----stdio.h" b
fwrite(${1:/*const char *ptr*/} , ${2:/*size_t size*/} ,${3:/* size_t nitems*/} , ${4:/*FILE *stream*/})
endsnippet
snippet fread "size_t <--fread <----stdio.h" b
fread(${1:/*void *ptr*/} , ${2:/*size_t size*/} ,${3:/* size_t nitems*/} , ${4:/*FILE *stream*/})
endsnippet
######################################################################
# magc for Kernel
######################################################################
snippet MODULE_LICENSE  "MODULE_LICENSE" b
MODULE_LICENSE("GPL");
endsnippet
snippet MODULE_AUTHOR  "MODULE_AUTHOR" b
MODULE_AUTHOR("guicai.ma@qq.com");
endsnippet
snippet MODULE_DESCRIPTION  "MODULE_DESCRIPTION" b
MODULE_DESCRIPTION("${0:/*模块描述*/}");
endsnippet

snippet module_frame  "module 模板" b
static int __intit ${1/([A-Za-z0-9_]+).*/$1/}_init(void)
{
	return 0;
}
static void __exit ${1/([A-Za-z0-9_]+).*/$1/}_exit(void)
{

}
module_init(${1/([A-Za-z0-9_]+).*/$1/}_init);
module_exit(${1:SYMBOL}_exit);
endsnippet
snippet module_param  "定义模块参数，类型为type" b
module_param(${1:name},${2:type},${3:permission});
endsnippet
snippet module_param_named "定义模块参数，参数名内外不同" b
module_param_named(${1:name},${2:variable},${3:type},${4:permission});
endsnippet
snippet module_param_string "定义模块字符串参数，并支持将命令行字符串参数直接复制到程序中的字符数组中" b
module_param_string(${1:name},${2:string},${3:len},${4:permission});
endsnippet
snippet module_param_array "定义模块参数数组，并支持将命令以，分隔的参数序列传递给该模块参数" b
module_param_array(${1:name},${2:type},${3:nump},${4:permission});
endsnippet
snippet MODULE_PARM_DESC  "模块参数描述" b
MODULE_PARM_DESC(${1:name},${2:Description});
endsnippet
snippet printk  "printk输出"
printk(${1:KERN_DEBUG} "${2:%s\n}"${2/([^%]|%%)*(%.)?.*/(?2:, :\);)/}$3${2/([^%]|%%)*(%.)?.*/(?2:\);)/}
endsnippet
snippet EXPORT_SYMBOL  "从模块中导出内核符号" b
EXPORT_SYMBOL(${1:SYMBOL});
endsnippet
snippet INIT_LIST_HEAD  "初始化链表头结点" b
INIT_LIST_HEAD(${1:struct list_head *list})
endsnippet
# vim:ft=snippets:
